const NodeMediaServer = require('node-media-server');
const { exec } = require('child_process');

function startnms(params) {
  const config = {
    rtmp: {
      port: 1935,
      chunk_size: 60000,
      gop_cache: true,
      ping: 30,
      ping_timeout: 60
    },
    http: {
      port: 8000,
      mediaroot: './media',
      allow_origin: '*'
    },
    trans: {
      ffmpeg: 'C:/zxn/ffmpeg-master-latest-win64-gpl-shared/bin/ffmpeg.exe',
      tasks: [
        {
          app: 'live',
          hls: true,
          hlsFlags: '[hls_time=2:hls_list_size=3:hls_flags=delete_segments]',
          hlsKeep: true, // to prevent hls file delete after end the stream
          dash: true,
          dashFlags: '[f=dash:window_size=3:extra_window_size=5]',
          dashKeep: true // to prevent dash file delete after end the stream
        }
      ]
    }
  };

  var nms = new NodeMediaServer(config)
  nms.run();

  exec('ffmpeg.exe -re -stream_loop -1 -i ./sampledata/a.mp4 -c copy -f flv rtmp://localhost/live/a', (error, stdout, stderr) => {
    if (error) {
      console.error(`执行错误: ${error}`);
      return;
    }
    // console.log(`stdout: ${stdout}`);
    console.error(`stderr: ${stderr}`);
  })


}

exports.startnms = startnms